// pages/address/address.js
var app = getApp();
var url = app.globalData.url;
var userId;
var address_id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiden: 'hiden',
    hidens: '',
    site: '    请选择地区',
    resslist: [],
    select: 0,
    province: '',//省级地区
    city: '',//市级地区
    county: '',//县级地区
    old_name: '',
    old_phone: '',
    old_ress: '',
    old_address: '',
    old_province: '',//旧的省级地区id
    old_city: '',//旧的市级地区id
    old_county: '',//旧的县级地区id
    ress_submit: 'ress_submit',//表单提交方式，默认添加新地址

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    userId = wx.getStorageSync('user_id');
    app.getUrl(options);
    var that = this
    if (options.id) {
      address_id = options.id
      var val = wx.getStorageSync('user_id');
      if (val) {
        console.log('有登陆');
        wx.request({
          url: url + '/api/user/address_list',
          method: 'POST',
          data: {
            user_id: val
          },
          success: function (res) {
            console.log(res.data.lists);
            var x = 0;
            for (x in res.data.lists) {
              if (res.data.lists[x].address_id == options.id) {
                console.log(res.data.lists[x]);
                that.setData({
                  old_name: res.data.lists[x].consignee,
                  site: res.data.lists[x].province_cn + ' ' + res.data.lists[x].city_cn + ' ' + res.data.lists[x].district_cn,
                  old_phone: res.data.lists[x].mobile,
                  old_address: res.data.lists[x].address,
                  old_province: res.data.lists[x].province,
                  old_city: res.data.lists[x].city,
                  old_county: res.data.lists[x].district,
                  address_id: res.data.lists[x].address_id
                })
                console.log(that.data)
              }
            }
            that.setData({
              sitelist: res.data.lists,
              ress_submit: 'edit_submit'
            })
          }
        })
      } else {
        console.log('没有登陆');

      }
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 选择地区
   */
  setRess: function () {
    var that = this
    wx.request({
      url: url + '/api/user/find_province',
      success: function (res) {
        console.log(res.data.province)
        that.setData({
          resslist: res.data.province,
          select: 0,
          hiden: '',
          hidens: 'hiden'
        })
      }
    })
  },
  select: function (e) {
    var that = this
    console.log(e.currentTarget.dataset.name);
    if (e.currentTarget.dataset.select == 0) {

      wx.request({
        url: url + '/api/user/find_city',
        data: {
          province_id: e.currentTarget.dataset.id
        },

        success: function (res) {
          console.log(res.data.city);

          that.data.site = e.currentTarget.dataset.name + ' '
          that.setData({
            resslist: res.data.city,
            select: 1,
            province: e.currentTarget.dataset.id
          })
        }
      })

    } else if (e.currentTarget.dataset.select == 1) {
      wx.request({
        url: url + '/api/user/find_region',
        data: {
          city_id: e.currentTarget.dataset.id
        },
        success: function (res) {
          console.log(res.data);
          that.data.site += e.currentTarget.dataset.name + ' '
          that.setData({
            resslist: res.data.region,
            select: 2,
            city: e.currentTarget.dataset.id
          })
        }
      })
    } else if (e.currentTarget.dataset.select == 2) {
      that.data.county = e.currentTarget.dataset.name
      //   that.data.site = that.data.province + "," + that.data.city + "," + that.data.county
      that.data.site += e.currentTarget.dataset.name
      that.setData({
        select: 0,
        county: e.currentTarget.dataset.id,
        hiden: 'hiden',
        hidens: '',
        site: that.data.site
      })

    }
  },
  /**
   * 表单提交事件
   */
  ress_submit: function (e) {
    var that = this;
    var val = wx.getStorageSync('user_id');
    // console.log(e.detail.value);return false;
    if (val) {
      console.log('有登 陆');
      if (e.detail.value.name != '') {
        if (e.detail.value.phone != '') {
          if (that.data.site != '    请选择地区') {
            if (e.detail.value.address != '') {
              wx.request({

                url: url + '/api/user/add_address',
                method: 'POST',
                data: {
                  user_id: val,
                  data: {
                    consignee: e.detail.value.name,
                    province: that.data.province,
                    city: that.data.city,
                    district: that.data.county,
                    address: e.detail.value.address,
                    mobile: e.detail.value.phone,
                    is_default: e.detail.value.is_default["0"],
                  }

                },
                success: function (res) {
                  console.log(res.data.msg);
                  if (res.data.msg == '添加成功') {
                    wx.showToast({
                      title: res.data.msg,
                      icon: 'success',
                      duration: 2000,
                      mask:true,
                      success:function(){
                        console.log(getCurrentPages());
                        wx.navigateBack({
                          delta: 1
                        })
                        // wx.redirectTo({
                        //   url: '/pages/sitelist/sitelist',
                        // })
                      }
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: res.data.msg
                    })
                  }
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '详细地址不能为空',
                success: function (res) {
                  return;
                }
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              content: '请选择地区',
              success: function (res) {
                return;
              }
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '电话不能为空',
            success: function (res) {
              return;
            }
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: '姓名不能为空',
          success: function (res) {
            return;
          }
        })
      }

    } else {
      console.log('没有登陆');

    }
  },
  /**
   * 编辑地址提交表单
   */
  edit_submit: function (e) {
    var val = wx.getStorageSync('user_id');
    var that = this
    var consignee;//收货人
    var province;//省份ID
    var city;//城市ID
    var district;//县地区ID
    var address;//详细地址
    var mobile;//手机号码
    if (val) {
      console.log('有登陆');
      if (e.detail.value.name == '') {
        consignee = that.data.old_name
      } else {
        consignee = e.detail.value.name
      }
      /*判断是否更改姓名*/
      if (e.detail.value.phone == '') {
        mobile = that.data.old_phone
      } else {
        mobile = e.detail.value.phone
      }
      /*判断是否更改手机号*/
      if (that.data.province == '' || that.data.city == '' || that.data.county == '') {
        province = that.data.old_province
        city = that.data.old_city
        district = that.data.old_county
      } else {
        province = that.data.province
        city = that.data.city
        district = that.data.county
      }
      /*判断是否更改地区*/

      if (e.detail.value.address == '') {
        address = that.data.old_address
      } else {
        address = e.detail.value.address
      }

      wx.request({
        url: url + '/api/user/edit_address',
        method: 'POST',
        data: {
          user_id: val,
          address_id: address_id,
          data: {
            consignee: consignee,
            province: province,
            city: city,
            district: district,
            address: address,
            mobile: mobile,
          }

        },
        success: function (res) {
          console.log(res.data.msg);
          if (res.data.msg == '编辑成功') {
            wx.reLaunch({
              url: '/pages/sitelist/sitelist',
            })
          }
        }
      })

    } else {
      console.log('没有登陆');

    }
  },
})